<%@page import="java.text.DecimalFormat" %>
<%@page import="java.util.*" %>
<%@ page import="java.text.DateFormat" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.cdvtc.flower.model.Customer" %>
<%@ page import="com.cdvtc.flower.model.Order" %>
<%@ page import="com.cdvtc.flower.dao.OrderDao" %>
<%@ page import="com.cdvtc.flower.dao.impl.OrderDaoImpl" %>
<%@ page import="com.cdvtc.flower.model.Orderdetail" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    DecimalFormat dcf = new DecimalFormat("#.00");
    request.setAttribute("df", df);
    request.setAttribute("dcf", dcf);

    Customer customer = (Customer) request.getSession().getAttribute("customer");
    List<Order> orders = null;
    if (customer != null) {
        request.setAttribute("customer", customer);
        OrderDao orderDao = new OrderDaoImpl();
        orders = orderDao.customerOrders(customer.getCustomer_id());
    } else {
        response.sendRedirect("login.jsp");
    }
%>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/all.min.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
</head>
<body>
<!-- 添加返回按钮 -->
<div class="container-fluid mt-3">
    <div class="row">
        <div class="col-12 text-left">
            <a href="index.jsp" class="btn btn-primary">返回</a>
            <!-- 或者你可以指定一个具体的URL，比如返回主页 -->
            <!-- <a href="/" class="btn btn-primary">返回主页</a> -->
        </div>
    </div>
</div>
<div class="container">
    <div class="card-header my-3">所有订单</div>
    <table class="table table-light">
        <thead>
        <tr>
            <th scope="col">时间</th>
            <th scope="col">总价</th>
            <th scope="col">取消</th>
        </tr>
        </thead>
        <tbody>

        <%
            if (orders != null) {
                for (Order o : orders) {%>
        <tr>
            <td>
                <%=df.format(o.getOrder_date_time()) %>
            </td>
            <td>
                ￥<%=dcf.format(o.getTotal_price()) %>
            </td>
            <td><a class="btn btn-sm btn-danger" href="cancel-order?id=<%=o.getOrder_id()%>">取消订单</a></td>
        </tr>
        <tr>
            <td colspan="3">
                <table class="table table-striped">
                    <% for (Orderdetail orderdetail : o.getOrderdetailList()) {%>
                    <tr>
                        <td><img class="img-thumbnail" width="100px"
                                 src="tool/assets/images/product/<%=orderdetail.getProduct().getImage()%>"></td>
                        <td><%=orderdetail.getProduct().getProduct_name()%>
                        </td>
                        <td>￥<%=orderdetail.getSale_price()%>
                        </td>
                        <td><%=orderdetail.getQuantity()%>
                        </td>
                        <td>￥<%=orderdetail.getTotal_price()%>
                        </td>
                    </tr>
                    <%}%>
                </table>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
</div>
<script src="${pageContext.request.contextPath}/js2/jquery-3.2.1.slim.min.js"></script>
<script src="${pageContext.request.contextPath}/js2/popper.min.js"></script>
<script src="${pageContext.request.contextPath}/js2/bootstrap.min.js"></script>
</body>
</html>
